<template>
  <div class="edit-box">
    <el-form label-width="120px" class="form-grid" :model="selfForm">



      <div class="form-box" v-if="technician_type==1">
        <universal-table :columns="columns" :fetch-data="fetchTableData" permission-prefix="order:service"
          :show-selection="true" :show-index="true" :init-search-form="{}" @edit="handleEdit" :bt1Name="'分配邻工'"
          @reset="reset" ref="table">


        </universal-table>
      </div>



    </el-form>
  </div>
</template>

<script>
  import UniversalTable from "@/components/TableComponent.vue";

  import {
    SERVICE_STATUS,SEX


  } from '@/constants/statusMap';
  export default {
    components: {
      UniversalTable,

    },
    props: {

      formData: {
        type: Object,
        required: true
      },
    },
    data() {
      return {

        cate_id:0,
        order_id:this.formData.id,
        selfForm:{},
        technician_type: 1,
        SERVICE_STATUS,
        SEX,
        currentRow: null,
        approvedOptions:[],
        columns: [{
            prop: "real_name",
            label: "真实姓名"
          },

          {
            prop: "sex",
            label: "性别",
            tag: true,
            tagConfig: SEX


          },

          {
            prop: "tel",
            label: "电话"
          },

          {
            prop: "id_card_number",
            label: "身份证号"
          },

          {
            prop: "p_title",
            label: "所在省"
          },
          {
            prop: "c_title",
            label: "所在市"
          },
          {
            prop: "a_title",
            label: "所在区|县"
          },

          {
            prop: "service_status",
            label: "接单状态",
            tag: true,
            tagConfig: SERVICE_STATUS
          },
          {
            prop: "create_time",
            label: "创建时间"
          },
        ],
        searchForm: {
          approved_status: 1,
        },

      };
    },

    mounted() {
      // 确保组件挂载后刷新一次数据
      this.$nextTick(() => {
        if (this.$refs.table) {
          console.log('组件挂载时刷新表格数据');
          this.$refs.table.refresh();
        }
      });
    },
    
    watch: {
      formData: {
        handler(newVal) {
          if (newVal) {
            console.log('formData 变化，强制刷新表格:', newVal);
            this.$nextTick(() => {
              // 强制刷新表格数据
              if (this.$refs.table) {
                this.$refs.table.refresh();
              }
            });
          }
        },
        immediate: true,
        deep: true
      }
    },


    methods: {
      // 获取表格数据
      async fetchTableData(params = {}) {
        this.searchForm.city_name = this.formData.city
        const postData = Object.assign({}, params, this.searchForm, {
          _t: Date.now() // 添加时间戳避免缓存
        });
        console.log('----request-postData----',  postData)
        try {
          const result = await this.$httpRequest.post('/neighbor_worker/list', postData)
          console.log('-------fetchTableData----', result)
          
          // 根据订单类型过滤虚拟临工（船到配送平台）
          if (result && result.list) {
            const orderType = this.formData.type;
            console.log('订单类型:', orderType);
            
            // 如果订单类型不是1或2，则过滤掉虚拟临工
            if (![1, 2].includes(parseInt(orderType))) {
              result.list = result.list.filter(worker => {
                // 过滤掉船到配送平台（虚拟临工）
                const isVirtualWorker = worker.tel === '40012345678' && worker.real_name === '船到配送平台';
                if (isVirtualWorker) {
                  console.log('过滤掉虚拟临工 - 订单类型不符合条件:', orderType);
                }
                return !isVirtualWorker;
              });
            } else {
              console.log('订单类型符合条件，显示虚拟临工:', orderType);
            }
          }
          
          return result
        } catch (e) {
          this.$message.error(e.message)
          throw e // 保持错误继续向上传递
        }
      },
      //重置
      reset() {
        this.searchForm.approved_status = 1;

      },
      // 编辑
      handleEdit(row) {

        this.currentRow = {
          ...row
        }; // 可深拷贝

        let postParam={}
        postParam.id=this.order_id*1;
        postParam.neighbor_worker_id = this.currentRow.id*1;


        this.$emit("assigning_order",postParam);
        //console.log('edit-----postParam', postParam)
      },







    },
  };
</script>

<style scoped>
  .bt-box{
    padding-left: 50px;
  }
  .form-box {
    display: flex;
  }
</style>
